home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 173_01 / hword.lxi < prev    next >
Text File  |  1980-01-01  |  3KB  |  112 lines

  1.  
  2. /*
  3.  * Word recognizer (with hyphenation)
  4.  *
  5.  * This program acts as a very simple filter for files of
  6.  * text that may have hyphenated words at the end of an input
  7.  * line.  Output consists of each word on a seperate line
  8.  * with hyphenated words rejoined.  Note: a word is said to
  9.  * start with the first alphabetic character and end with the
  10.  * last alphabetic character.  Embedded graphics will be removed.
  11.  */
  12.  
  13. /*
  14.  * Basic elements
  15.  */
  16. white   = [\n\r\t ];            /* End of a word        */
  17. bol     = [\n] white*;          /* Beginning of a line  */
  18. eol     = [\0\n\r];             /* End of input line    */
  19. letter  = [A-Za-z];             /* Is  a letter         */
  20. graphic = [!-@\[-`{-~];         /* Not a letter         */
  21. text    = [!-~];                /* All printing chars.  */
  22. garbage = [\1-\377];            /* Whatever remains     */
  23.  
  24. /*
  25.  * A word contains "junk", at least one letter, then at
  26.  * least another letter, then more junk.
  27.  *
  28.  * A hyphenated word is a word-<NEWLINE> followed by a word
  29.  * on the next line.
  30.  */
  31.  
  32. word    = graphic* letter text* letter graphic*;
  33. junk    = (letter white) | (graphic* white);
  34.  
  35. %{
  36. #define TRUE 1
  37. #define FALSE 0
  38.  
  39. main()
  40. {
  41.         while (yylex())
  42.                 ;
  43. }
  44. %}
  45.  
  46. %%
  47.  
  48. /*
  49.  * A hyphenated word
  50.  */
  51.  
  52. word "-" eol / bol letter letter
  53.         {
  54.                 output(TRUE);
  55.                 return(LEXSKIP);
  56.         }
  57. /*
  58.  * An ordinary word
  59.  */
  60.  
  61. word    {
  62.                 output(FALSE);
  63.                 return(LEXSKIP);
  64.         }
  65.  
  66.  
  67. /*
  68.  * Junk (one letter words or all graphics)
  69.  */
  70.  
  71. junk
  72.         {
  73.                 return(LEXSKIP);
  74.         }
  75.  
  76. /*
  77.  * Other stuff
  78.  */
  79.  
  80. eol | white | garbage
  81.         {
  82.                 return(LEXSKIP);
  83.         }
  84.  
  85. %%
  86.  
  87. output(flag)
  88. int             flag;
  89. /*
  90.  * Output the current token.  The parameter is TRUE if this is
  91.  * the start of a hyphenated word.
  92.  */
  93. {
  94.         register char   *tokptr;        /* Locate token start   */
  95.         char            *tokend;        /* Locate token end     */
  96.         char    *token();
  97.         char buffer[100]; 
  98.  
  99.         tokptr = token(&tokend);
  100.         /*
  101.          * Skip over leading and trailing non-alpha stuff
  102.          */
  103.         while (!isalpha(*tokptr) && tokptr < tokend)
  104.                 tokptr++;
  105.         while (!isalpha(*--tokend) && tokend > tokptr);
  106.         strncpy(buffer, tokptr, tokend-tokptr+1);
  107.         buffer[tokend-tokptr+1]='\0';
  108.         printf("%s", buffer);
  109.         if (!flag)
  110.                 putchar('\n');
  111. }
  112.